<?php
require_once 'pg_info.php';
require_once 'classes/users.php';

class PGSession{
	
	static $userLogged = "pg_user_logged";
	
	function isUserLoggedIn(){
		return(isset($_SESSION[self::$userLogged]) && ($_SESSION[self::$userLogged] instanceof User));
	}
	
	private function start_pg_session(){
		global $pg_sessionTimeout, $pg_sessionProb, $pg_sessionDivisor;
		#SET TIMEOUT SESSION TIME TO X SECONDS
		ini_set("session.gc_maxlifetime",$pg_sessionTimeout);
		ini_set("session.gc_probability",$pg_sessionProb);
		ini_set("session.gc_divisor",$pg_sessionDivisor);
		#CHECK SESSION TO USER ALREADY LOGGED IN
		session_start();
	}
	
	function __construct(){
		$this->start_pg_session();
	}
	
	function redirect($url){
		if (headers_sent()){
			exit('<script type="text/javascript">window.location.href="' . $url . '";</script>');
		}else{
			header('Location: ' . $url);
			exit();
		}
	}
	
	function getUser(){
		return($_SESSION[self::$userLogged]);
	}
	
	function getUserId(){
		return($_SESSION[self::$userLogged]->getId());
	}
	
	function getUserName(){
		return($_SESSION[self::$userLogged]->getName());
	}
	
	function getUserEmail(){
		return($_SESSION[self::$userLogged]->getEmail());
	}
	
	function saveUserInSession($user){
		$_SESSION[self::$userLogged] = $user;
	}
	
	function removeUserInSession(){
		session_unset();
	}
}

$pgSession = new PGSession();